/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['siteInfo:add','siteInfo:update'],//本页权限数组
        selects:[],
        description:'',
        icp:'',
        networksecurity:'',
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);

            return ;
        }
        that.getInfoById(aGET.id);
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        getInfoById:function (id) {
            var that = this;
            Get(that,_HOST+_API.common_siteInfo_getDetail+"?companyId="+companyId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            var list = response.data;
                            for (var item in list) {
                                if (item == 'networksecurity') {
                                    that.networksecurity = list[item];
                                }
                                if (item == 'description') {
                                    that.description = list[item];
                                }
                                if (item == 'icp') {
                                    that.icp = list[item];
                                }
                                $("#Form input[name='" + item + "']").val(list[item]);
                            }
                            $("#Form").data('bootstrapValidator').destroy();
                            $('#Form').data('bootstrapValidator', null);
                            Validator();
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        // 添加
        add:function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            //console.log(param)
            param['companyId'] = companyId;
            param['schoolId'] = schoolId;
            Post(that,_HOST+_API.common_siteInfo_add,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
        // 修改
        update:function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            // console.log(param)
            param['companyId'] = companyId;
            param['schoolId'] = schoolId;
            Post(that,_HOST+_API.common_siteInfo_update,param,true,that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
    },
});


$('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '网站名称不能为空'
                    },
                    stringLength: {
                        max: 50,
                        message: '网站名称长度最多50个字符'
                    },
                }
            },
            tel: {
                validators: {
                    notEmpty: {
                        message: '官方电话不能为空'
                    },
                    stringLength: {
                        max: 15,
                        message: '官方电话长度最多15个字符最少3个字符'
                    },
                    /*  stringLength: {
                          min: 3,
                          message: '官方电话长度最少3个字符'
                      },*/
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: '公司邮箱不能为空'
                    },
                    stringLength: {
                        max: 20,
                        message: '邮箱长度最多20个字符'
                    },
                    emailAddress:{
                        message: '邮箱格式不正确'
                    }
                }
            },facsimile: {
                validators: {
                    notEmpty: {
                        message: '公司传真不能为空'
                    },
                    stringLength: {
                        max: 20,
                        message: '传真长度最多20个字符'
                    },
                }
            },
            address: {
                validators: {
                    notEmpty: {
                        message: '公司地址不能为空'
                    },
                    stringLength: {
                        max: 500,
                        message: '公司地址长度最多500个字符'
                    },
                }
            }, url: {
                validators: {
                    notEmpty: {
                        message: '网站访问地址不能为空'
                    },
                    stringLength: {
                        max: 50,
                        message: '网站访问地址长度最多50个字符'
                    },
                }
            },
            keywords: {
                validators: {
                    notEmpty: {
                        message: '关键词不能为空'
                    },
                    stringLength: {
                        max: 500,
                        message: '关键词长度最多500个字符'
                    },
                }
            },
            description: {
                validators: {
                    notEmpty: {
                        message: '描述不能为空'
                    },
                    stringLength: {
                        max: 500,
                        message: '描述长度最多500个字符'
                    },
                }
            },
            icp: {
                validators: {
                    notEmpty: {
                        message: '域名备案号信息不能为空'
                    },
                }
            },
            networksecurity: {
                validators: {
                    notEmpty: {
                        message: '网安备案信息不能为空'
                    },
                }
            },
            copyright: {
                validators: {
                    notEmpty: {
                        message: '版权信息不能为空'
                    },
                    stringLength: {
                        max: 999,
                        message: '版权信息长度最多999个字符'
                    },
                }
            },
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        if(empty(aGET.id)){
            app.add();
        }else {
            app.update();
        }
    });
}
